# -*-coding:utf-8 -*-
import requests

from . import config


class YBApi:

    def __init__(self):
        self.username = config.YB['username']
        self.password = config.YB['password']
        self.base_url = config.YB['host']
        self.headers = {'Content-Type': 'application/json', 'accept': 'application/json'}
        self.set_token()

    def set_token(self):
        """
        登录获得token,并更新到headers
        :return:
        """
        token = requests.post(f'{self.base_url}/user/login', json={
            "username": self.username,
            "password": self.password,
        }, headers=self.headers).json()['token']

        self.headers.update({
            'Authorization': f'token {token}'
        })

    def add_vehicle(self, vehicle):
        """
        向后台添加车辆
        :return:
        """
        data = {
            "vehicle": {
                "license_number": vehicle['car_id'],
                "vin": vehicle['uuid'],
            },
            "company": {
                "name": '开易'  # 新车辆同一配置给开易公司名下
            },
            "terminal": {
                "imei": vehicle['imei'],
                'firmware_version': f'{vehicle["version"]}',
                'device_type': 'KY'
            }
        }
        return requests.post(f'{self.base_url}/company/vcts', json=data, headers=self.headers)

    def add_xz_vehicle(self, data):
        """
        向后台添加小卓设备车辆
        :return:
        """
        return requests.post(f'{self.base_url}/company/vcts', json=data, headers=self.headers)

    def get_vehicles(self):
        """
        获取yb后台的车辆列表
        :return:
        做vin和vehicle的关系映射
        """
        page = 1
        vehicle_infos = {}
        while True:
            res = requests.get(f'{self.base_url}/company/vcts', params={
                'page': page,
                'page_size': 100,
            }, headers=self.headers).json()
            for infos in res['results']:
                vehicle_infos[infos['terminal']['imei']] = {
                    'terminal_id': infos['terminal']['id'],
                    'vehicle_id': infos['vehicle']['id'],
                    'company_id': infos['company']['id'],
                    'license_number': infos['vehicle']['license_number'],
                    'vin': infos['vehicle']['vin'],
                    'device_id': infos['terminal']['imei'],
                    'imei': infos['terminal']['imei']
                }
            if not res['next']:  # 没有下一页
                break
            page += 1
        return vehicle_infos

    def send_alarm(self, data):
        """
        发送报警信息
        """
        res = requests.post(f'{self.base_url}/alarm/alarms',
                            json=data,
                            headers=self.headers)
        return res

    def add_history_video(self, data):
        """
        发送报警信息
        """
        res = requests.post(f'{self.base_url}/media/medias',
                            json=data,
                            headers=self.headers)
        return res

    def update_vehicle_status(self, vehicle_id, data):
        res = requests.patch(f'{self.base_url}/location/vehicle-statuses/{vehicle_id}',
                             json=data,
                             headers=self.headers)
        return res
